package com.zhhs.galaxyott.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.android.volley.VolleyError;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.DeviceCenterActivity;
import com.zhhs.galaxyott.adapters.WatchGridAdapter;
import com.zhhs.galaxyott.beans.WatchHistoryBean;
import com.zhhs.galaxyott.http.api.RequestMaker;
import com.zhhs.galaxyott.httpVolley.base.ResponseListener;
import com.zhhs.galaxyott.httpVolley.response.WatchHistoryResponse;
import com.zhhs.galaxyott.sharepreference.SharedPrefHelper;
import com.zhhs.galaxyott.utils.Logs;

public class WatchRecordFragment extends BaseFragment {

    private static String TAG = WatchRecordFragment.class.getSimpleName();

    private static WatchRecordFragment f;

    private static View view;
    private static GridView mGridView;
    private WatchGridAdapter mAdapter;
    private static List<WatchHistoryBean> mlistData;

    private SharedPrefHelper sp;

    public static WatchRecordFragment newInstance() {
        if (f == null) {

            f = new WatchRecordFragment();
        }
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = SharedPrefHelper.getInstance(getActivity());

        loadData();
    }

    private void loadData() {
        mlistData = new ArrayList<WatchHistoryBean>();
        String smartcardid = sp.getsmartcard();
        int pageNo = 1;
        int pageRow = 100;

        RequestMaker.getWatchRecord(getActivity(), smartcardid, pageNo, pageRow,
                new ResponseListener<WatchHistoryResponse>() {

                    @Override
                    public void onResponse(WatchHistoryResponse result) {

                        if (result.Beans == null || result.Beans.size() == 0) {
                            return;
                        }

                        Logs.logD("DEBUG_DT",
                                "THIS IS getWatchRecord-->" + result.errCode + "SIZE" + result.Beans.size());

                        if (result.errCode == 0) {

                            mlistData = result.Beans;

                            mAdapter = new WatchGridAdapter(getActivity(), mlistData);
                            mGridView.setAdapter(mAdapter);

                        }
                    }

                    @Override
                    public void onError(VolleyError error) {
                        Logs.logD("DEBUG_DT", "THIS IS getWatchRecord-->ERR");

                    }
                });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_watchrecords, container, false);

        mGridView = (GridView) view.findViewById(R.id.records_grid);

        mGridView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        mGridView.setFocusable(false);

        return view;
    }

    public static boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            Logs.logD(TAG, "THIS IS onKeyDown-->0");
            if (mlistData.size() == 0) {
                DeviceCenterActivity.titlelayout.getChildAt(DeviceCenterActivity.index).setBackgroundResource(
                        R.drawable.title_focus);
                return true;
            }
            if (mGridView.findFocus() == null) {
                Logs.logD("DEBUG_1", "THIS IS onKeyDown-->2");
                DeviceCenterActivity.detailFocus = true;
                mGridView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
                mGridView.requestFocus();
                Logs.logD("DEBUG_1", "THIS IS onKeyDown-->3");
                return true;
            }
            break;

        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (view.findFocus() != null && isLeftView()) {
                Logs.logD(TAG, "THIS IS onKeyDown-->KEYCODE_DPAD_LEFT");
                DeviceCenterActivity.detailFocus = false;
                mGridView.setDescendantFocusability(GridView.FOCUS_BLOCK_DESCENDANTS);
                mGridView.clearFocus();
            }
            break;

        case KeyEvent.KEYCODE_DPAD_DOWN:
            break;
        default:
            break;
        }
        return false;

    }

    private static boolean isLeftView() {
        for (int i = 0; i < (((mlistData.size() - 1) / 5) + 1); i++) {
            if (mGridView.getChildAt(5 * i).hasFocus()) {
                return true;
            }
        }
        return false;
    }
}
